#include "StdAfx.h"
#include "CudaDeviceBuffer.h"
using namespace System;

namespace GRIP
{
	namespace Cuda
	{
		InternalCudaDeviceBuffer::InternalCudaDeviceBuffer(int size)
		{
			CudaErrorCheck(cudaMalloc((void**) &buffer, size));
		}
		
		InternalCudaDeviceBuffer::~InternalCudaDeviceBuffer()
		{
			CudaErrorCheck(cudaFree(buffer));
		}

		CudaDeviceBuffer::CudaDeviceBuffer(int width, int height, int elementSize)
		{
			Width = width;
			Height = height;
			Size = width * height * elementSize;
			InternalBuffer = new InternalCudaDeviceBuffer(Size);
			ManagedBufferPtr = IntPtr(InternalBuffer->buffer);
		}

		BYTE* CudaDeviceBuffer::Ptr()
		{
			return InternalBuffer->buffer;
		}

		IntPtr CudaDeviceBuffer::Data()
		{
			return ManagedBufferPtr;
		}

		int CudaDeviceBuffer::BufferSize()
		{
			return Size;
		}

		int CudaDeviceBuffer::GetWidth()
		{
			return Width;
		}

		int CudaDeviceBuffer::GetHeight()
		{
			return Height;
		}

		void CudaDeviceBuffer::ClearData()
		{
			CudaErrorCheck(cudaMemset(InternalBuffer->buffer, 0, Size));
		}
	}
}
